var $ = require('jquery');
var util = require('imutil');

var tpl = require('../tpl');
var i18n = require('../i18n');
var servant = require('../i18n/servant');
var bus = require('bus');
var dialog = require('dialog');

var defaults = {
    el: null,
    tmpl: tpl.chat2.blocknotice()
};

var Blocknotice = function(config,api,uid){
    this.config = util.extend({}, defaults, config);
    this.elems = {};
    this.api = api;
    this.uid = uid;
    this.__init();
    this._initEvent();
};

Blocknotice.prototype = {
    __init: function(){
        this.elems.$el = $(this.config.el);
        this.elems.$timestamp = $(this.config.tmpl);
        this.elems.$unblock = $(".J-im-unblock",this.elems.$timestamp);
        this.elems.$close = $(".J-im-icon-close",this.elems.$timestamp);

        if(this.api.sdk.isBlocked(this.uid)){
            this.elems.$el.append(this.elems.$timestamp);
        }

    },
    _initEvent: function(){
        var that = this;
        that.elems.$unblock.bind("click",function(){
            that.api.sdk.unblock(that.uid,function(data){
                that.elems.$el.remove();
                var cancelSuccessTip = dialog.tip( 'Remove contact fron the black list successfully')
                window.setTimeout(function(){
                    cancelSuccessTip.close();
                } , 3000);
            });
        });
        that.elems.$close.bind("click",function(){
            that.elems.$timestamp.css("display","none")
        });
    }
};

module.exports = Blocknotice;